//package com.ruibang.glass.quality.component.job;
//
//import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
//import com.ruibang.glass.quality.constant.Constants;
//import com.ruibang.glass.quality.entity.EveryHourColdDirectLoss;
//import com.ruibang.glass.quality.service.EveryHourColdDirectService;
//import com.ruibang.glass.quality.service.TotalEquDataService;
//import com.teaming.cloud.framework2.elastic.job3.annotation.ElasticJobScheduler;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.shardingsphere.elasticjob.api.ShardingContext;
//import org.apache.shardingsphere.elasticjob.simple.job.SimpleJob;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.time.LocalDate;
//import java.time.format.DateTimeFormatter;
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Map;
//import java.util.stream.Collectors;
//
///**
// * @Author: songJinKang
// * @CreateTime: 2024-01-30  09:26
// * @Description: TODO:冷端直投缺陷全检(每小时)
// * @Version: 1.0
// */
//
//@Slf4j
//@ElasticJobScheduler(cron = "7 40 0/1 * * ?",name = "EveryHourInvestmentDefectInspectionTask",shardingItemParameters = "0=0", jobParameters = "parameter")
////@ElasticJobScheduler(cron = "7 * * * * ?", name = "EveryHourInvestmentDefectInspectionTask", shardingItemParameters = "0=0", jobParameters = "parameter")
//@Component
//public class EveryHourInvestmentDefectInspectionTask implements SimpleJob {
//
//    @Autowired
//    private TotalEquDataService totalEquDataService;
//
//    @Autowired
//    private EveryHourColdDirectService everyHourColdDirectService;
//
//    @Override
//    public void execute(ShardingContext shardingContext) {
//        importEveryHourColdDirect(Constants.DEFECT_DEFAULT_QUERY_DAYS);
//    }
//
//    @Transactional(rollbackFor = Exception.class)
//    public void importEveryHourColdDirect(int dayNum) {
//        for (int i = 0; i < dayNum; i++) {
//            String today = LocalDate.now().minusDays(i).format(DateTimeFormatter.ISO_LOCAL_DATE);
//            List<EveryHourColdDirectLoss> datas = everyHourColdDirectService.queryTodayData(today);
//            List<EveryHourColdDirectLoss> todayData = totalEquDataService.queryEveryHourColdDirect(today);
//            if (CollectionUtils.isNotEmpty(todayData) && CollectionUtils.isNotEmpty(datas)) {
//                List<EveryHourColdDirectLoss> newDatas = new ArrayList<>();
//
//                if (CollectionUtils.isNotEmpty(todayData)) {
//                    todayData.forEach(d -> {
//                        String time = d.getDirectTime().format(DateTimeFormatter.ofPattern("yyyyMMddHH"));
//                        d.setHourTimeKey(time);
//                    });
//                    Map<String, EveryHourColdDirectLoss> todayMap = todayData.stream().collect(Collectors.toMap(EveryHourColdDirectLoss::getHourTimeKey, d -> d, (d1, d2) -> d1));
//                    datas.forEach(d -> {
//                        String time = d.getDirectTime().format(DateTimeFormatter.ofPattern("yyyyMMddHH"));
//                        d.setHourTimeKey(time);
//                    });
//
//                    Map<String, EveryHourColdDirectLoss> oldMap = datas.stream().collect(Collectors.toMap(EveryHourColdDirectLoss::getHourTimeKey, d -> d, (d1, d2) -> d1));
//                    oldMap.forEach((k, v) -> {
//                        EveryHourColdDirectLoss loss = todayMap.get(k);
//                        if (loss != null) {
//                            long defectNum = loss.getPlatinumTotalNum() + loss.getBubbleTotalNum() + loss.getStoneTotalNum();
//                            float platinumBoardNum = loss.getPlatinumTotalNum() / (float) loss.getCheckNum();
//                            float stoneBoardNum = loss.getStoneTotalNum() / (float) loss.getCheckNum();
//                            float bubbleBoardNum = loss.getBubbleTotalNum() / (float) loss.getCheckNum();
//                            float openBubbleBoardNum = loss.getOpenBubbleTotalNum() / (float) loss.getCheckNum();
//                            float defectBoardNum = defectNum / (float) loss.getCheckNum();
//                            loss.setDefectTotalNum(defectNum);
//                            loss.setPlatinumBoardNum(platinumBoardNum);
//                            loss.setStoneBoardNum(stoneBoardNum);
//                            loss.setBubbleBoardNum(bubbleBoardNum);
//                            loss.setDefectBoardNum(defectBoardNum);
//                            loss.setOpenBubbleBoardNum(openBubbleBoardNum);
//                            newDatas.add(loss);
//                            todayMap.remove(k);
//                        } else {
//                            newDatas.add(v);
//                        }
//                    });
//
//                    if (CollectionUtils.isNotEmpty(todayMap)) {
//                        todayMap.forEach((k, v) -> {
//                            newDatas.add(v);
//                        });
//                    }
//
//                    everyHourColdDirectService.removeByIds(datas.stream().map(EveryHourColdDirectLoss::getId).collect(Collectors.toList()));
//
//                    if (CollectionUtils.isNotEmpty(newDatas)) {
//                        everyHourColdDirectService.saveBatch(newDatas);
//                    }
//                }
//            } else {
//                if (CollectionUtils.isNotEmpty(todayData)) {
//                    for (EveryHourColdDirectLoss data : todayData) {
//                        if (data != null) {
//                            long defectNum = data.getPlatinumTotalNum() + data.getBubbleTotalNum() + data.getStoneTotalNum();
//                            float platinumBoardNum = data.getPlatinumTotalNum() / (float) data.getCheckNum();
//                            float stoneBoardNum = data.getStoneTotalNum() / (float) data.getCheckNum();
//                            float bubbleBoardNum = data.getBubbleTotalNum() / (float) data.getCheckNum();
//                            float openBubbleBoardNum = data.getOpenBubbleTotalNum() / (float) data.getCheckNum();
//                            float defectBoardNum = defectNum / (float) data.getCheckNum();
//                            data.setDefectTotalNum(defectNum);
//                            data.setPlatinumBoardNum(platinumBoardNum);
//                            data.setStoneBoardNum(stoneBoardNum);
//                            data.setBubbleBoardNum(bubbleBoardNum);
//                            data.setDefectBoardNum(defectBoardNum);
//                            data.setOpenBubbleBoardNum(openBubbleBoardNum);
//                        }
//                    }
//                    everyHourColdDirectService.saveBatch(todayData);
//                }
//            }
//        }
//    }
//
//}
